package com.android.launcher3;

import android.graphics.Canvas;
import android.os.SystemClock;
import android.view.View;
import android.view.animation.Interpolator;

public class DragAnimation {

	public static final String TAG = "DragAnimation";
	View mAnimationView;
	float mAnimationFromX;
	float mAnimationFromY;
	float mAnimationToX;
	float mAnimationToY;
	float mXOffset;
	float mYOffset;
	int mAnimationDuration;
	int mAnimationState = ANIMATION_STATE_DONE;
	long mAnimationStartTime;
	private Interpolator mInterpolator;
	private static final int ANIMATION_STATE_STARTING = 1;
	private static final int ANIMATION_STATE_RUNNING = 2;
	private static final int ANIMATION_STATE_DONE = 3;

	/**
	 * Creates a new DragAnimation object.
	 */
	DragAnimation() {
	}

	/**
	 * Creates a new DragAnimation object.
	 * 
	 * @param paramInterpolator
	 */
	DragAnimation(final Interpolator paramInterpolator) {
		this.mInterpolator = paramInterpolator;
	}

	public void setAnimation(final View animationView, final float fromX,
			final float toX, final float fromY, final float toY,
			final float xOffset, final float yOffset, final int duration) {
		this.mAnimationView = animationView;
		this.mAnimationFromX = fromX;
		this.mAnimationToX = toX;
		this.mAnimationFromY = fromY;
		this.mAnimationToY = toY;
		this.mXOffset = xOffset;
		this.mYOffset = yOffset;
		this.mAnimationDuration = duration;
		this.mAnimationState = ANIMATION_STATE_STARTING;
	}

	boolean draw(final Canvas canvas) {
		// Log.e(TAG,"DragAnimation draw");
		float f3;

		if (this.mAnimationView == null) {
			return true;
		}

		if (this.mAnimationState == ANIMATION_STATE_STARTING) {
			// Log.e(TAG,"DragAnimation ANIMATION_STATE_STARTING");
			this.mAnimationStartTime = SystemClock.uptimeMillis();
			this.mAnimationState = ANIMATION_STATE_RUNNING;

			// Log.e(TAG,"DragAnimation ANIMATION_STATE_STARTING "+ info.title);
		}

		if (this.mAnimationState == ANIMATION_STATE_RUNNING) {
			// Log.e(TAG,"DragAnimation ANIMATION_STATE_RUNNING");
			float f1 = (float) (SystemClock.uptimeMillis() - this.mAnimationStartTime);
			float f2 = this.mAnimationDuration;
			f3 = f1 / f2;

			if (f1 >= f2) {
				this.mAnimationState = ANIMATION_STATE_DONE;
				f3 = 1.0F;
			}

			if (this.mInterpolator != null) {
				f3 = this.mInterpolator.getInterpolation(f3);
			}

			float f7 = (this.mAnimationToX - this.mAnimationFromX) * f3;
			float f8 = this.mAnimationFromX + f7;
			float f13 = this.mAnimationFromY
					+ (this.mAnimationToY - this.mAnimationFromY) * f3;
			canvas.save();
			canvas.translate(f8, f13);
			this.mAnimationView.draw(canvas);
			canvas.restore();
		}

		if (this.mAnimationState == ANIMATION_STATE_DONE) {
			// Log.e(TAG,"DragAnimation ANIMATION_STATE_DONE");
			this.mAnimationView.setVisibility(View.VISIBLE);

			// ApplicationInfo info = (ApplicationInfo)
			// this.mAnimationView.getTag();

			// Log.e(TAG,"DragAnimation ANIMATION_STATE_DONE "+ info.title);
			return false;
		}

		return true;
	}
}
